package view.dialogs;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.UIManager;
import javax.swing.filechooser.FileFilter;

import model.Constants;

/**
 * @author Alexander Müller
 * 
 */
public class FileChooserDialog extends JFileChooser {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String filePath;

	public FileChooserDialog(int modus) {
		this.setDialogTitle(Constants.LOADERTITLE);
		addFileFilter();
		if(modus == 1){
			this.setApproveButtonText("Speichern");
		}

	}

	private void addFileFilter() {

		this.setFileFilter(new FileFilter() {

			@Override
			public String getDescription() {
				return Constants.XMLTYPES;
			}

			@Override
			public boolean accept(File f) {
				return f.isDirectory()
						|| f.getName().toLowerCase()
								.endsWith(Constants.XMLENDING);
			}
		});

	}
	
	public boolean openDialog() {

		int state = this.showOpenDialog(null);

		if (state == JFileChooser.APPROVE_OPTION) {

			filePath = this.getSelectedFile().getAbsolutePath();
			
			if(!validatePath()){
				new InfoDialog(Constants.ERROR,
						Constants.FILEERRORMESSAGE);
				return false;
			}

			return true;

		} else {

			return false;
		}

	}
	
	public String getFilePath() {
		return filePath;
	}

	public boolean validatePath() {
		return filePath.endsWith(Constants.XMLENDING) ? true : false;
	}
}